package com.ficv.findme;

import org.apache.cordova.CordovaChromeClient;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.DroidGap;
import org.apache.cordova.IceCreamCordovaWebViewClient;
import org.apache.cordova.api.CordovaInterface;

import android.os.Bundle;
import android.util.Log;
import android.webkit.JsResult;
import android.webkit.WebView;

public class MiDroidGap extends DroidGap{
		@Override
		public void onCreate(Bundle savedInstanceState)
		{
		    super.onCreate(savedInstanceState);
		    //super.loadUrl("file:///android_asset/www/index.html");
		}

		@Override
		public void init() {
		    Log.e(TAG, "init()");
		    CordovaWebView webView = new CordovaWebView(MiDroidGap.this);
		    CordovaWebViewClient webViewClient;
		    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
		    {
		        webViewClient = new CordovaWebViewClient(this, webView);
		    }
		    else
		    {
		        webViewClient = new IceCreamCordovaWebViewClient(this, webView);
		    }
		    this.init(webView, webViewClient, new MyCordovaChromeClient(this, webView));
		}

		private class MyCordovaChromeClient extends CordovaChromeClient{
		    private CordovaInterface cordova;
		    public MyCordovaChromeClient(CordovaInterface ctx, CordovaWebView app) {
		        super(ctx, app);
		        this.cordova = ctx;
		    }


		    @Override
		    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
		        if(cordova.getActivity().isFinishing()){
		            Log.w(TAG, "Trying to alert while activity is finishing!! -> ignore");
		            result.cancel();
		            return true;
		        }

		        return super.onJsAlert(view, url, message, result);
		    }
		}
		}
